package aries.database;

/******************************************************************************
 * You may modify, publish, and use Aries for your own purpose. BUT
 * DON'T REMOVE THIS TEXT.
 * Visit my facebook: http://www.facebook.com/freeskies or
 * Follow my twitter: @freeskys
 *
 *
 * Author: Harditya Rahmat Ramadhan
 * Licence: GPLv2
 *****************************************************************************/

import aries.utils.TextUtil;
import java.lang.reflect.Array;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

public class TableModel extends AbstractTableModel {

    /**
     * The number of column.
     */
    private int colnum = 0;
    
    /**
     * Name of column.
     */
    private String colNames[] = null;
    
    /**
     * ArrayList to save the data.
     */
    private ArrayList ResultSets;

    /**
     * Constructor.
     * 
     * @param colNames
     * @param rs
     * @throws Exception 
     */
    public TableModel(String colNames[], ResultSet rs) throws Exception {
        colnum = colNames.length;
        for(byte i = 0; i < colNames.length; i++) {
            colNames[i] = TextUtil.upperFirstCase(colNames[i]);
        }
        this.colNames = colNames;
        ResultSets = new ArrayList();
        try {
            String row[];
            for(; rs.next(); ResultSets.add(row)) {
                row = new String[colnum];
                for(byte i = 0; i < row.length; i++)
                    Array.set(row, i, rs.getString(colNames[i]));
            }
        } catch(Exception e) {
            throw e;
        }
    }

    /**
     * Get total row.
     * 
     * @return int
     */
    public int getRowCount() {
        return ResultSets.size();
    }

    /**
     * Get total column.
     * 
     * @return int
     */
    public int getColumnCount() {
        return colnum;
    }

    /**
     * Get value at specified row and column
     * 
     * @param rowIndex
     * @param columnIndex
     * @return String
     */
    public String getValueAt(int rowIndex, int columnIndex) {
        String row[] = (String[])ResultSets.get(rowIndex);
        return row[columnIndex].toString();
    }

    /**
     * Get name of the column.
     * 
     * @param param
     * @return String
     */
    @Override
    public String getColumnName(int param) {
        return colNames[param];
    }
    
    /**
     * Get the class name of the column.
     * 
     * @param c
     * @return Class
     */
    @Override
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    
    /**
     * Check wheter the cell is editable or not.
     * 
     * @param row
     * @param col
     * @return boolean
     */
    @Override
    public boolean isCellEditable(int row, int col) {
        if (col < 2) {
            return false;
        } else {
            return true;
        }
    }
    
    /**
     * Set the value on the table.
     * 
     * @param value
     * @param rowIndex
     * @param columnIndex 
     */
    @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        Array.set(rowIndex, columnIndex, value);
    }
}